<?php

include('database.php'); 
include("globalvars.php");

function curl_request_async($url, $params, $type='POST'){
	foreach ($params as $key => &$val) { if (is_array($val)) $val = implode(',', $val); $post_params[] = $key.'='.urlencode($val); }
	$post_string = implode('&', $post_params);
	$parts=parse_url($url);
	$fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);
	if('GET' == $type) $parts['path'] .= '?'.$post_string;
	$out = "$type " . $parts['path'] . " HTTP/1.1\r\n";
	$out.= "Host: " . $parts['host'] . "\r\n";
	$out.= "Content-Length: " . strlen($post_string) . "\r\n";
	$out.= "Connection: Close\r\n\r\n";
	if ('POST' == $type && isset($post_string)) $out.= $post_string;
	fwrite($fp, $out);
	fclose($fp);
}

$userAccounts = array();
$result = mysql_query("SELECT * FROM users");
while ($row = mysql_fetch_array($result)) {
	$userAccounts[$row['facebookid']] = $row; 
}

$result = mysql_query("SELECT * FROM notifications WHERE notifydate LIKE '%1%'");
while ($row = mysql_fetch_array($result)) {
	
	date_default_timezone_set($zonelist[$userAccounts[$row['facebookid']]['timezone']]);
	
	$notificationParams = $row['notifydate'];
	$notificationTimes = array();
	for($i = 0; $i < strlen($notificationParams); $i++){
		if ($notificationParams[$i] == '1'){
			switch($i){
				case 0:
					$hours = 1;
					break;
				case 1:
					$hours = 24;
					break;
				case 3:
					$hours = 48;
					break;
				case 4:
					$hours = 72;
					break;
			}
			$timezone = new DateTimeZone($zonelist[$userAccounts[$row['facebookid']]['timezone']]);
			$sendDate = new DateTime(date("Y-m-d H:i:s", (int)$row['appointmentdate'] - ($hours * 60 * 60)), $timezone);
			$now = new DateTime;
			
			if ($now > $sendDate){
				$notificationParams[$i] = '2';
				$msg = str_replace('{date}', date('l\, F jS \a\t h:i A' , $row['appointmentdate']), $row['format']);
				$msg = str_replace('{company}', $userAccounts[$row['facebookid']]['company'], $msg);
				$msg = str_replace('{your name}', $userAccounts[$row['facebookid']]['username'], $msg);
				$msg = str_replace('{client first name}', $row['clientnamefirst'], $msg);
				$msg = str_replace('{client last name}', $row['clientnamelast'], $msg);
				$msg = str_replace('{appointment}', $row['appointmentbody'], $msg);
				
				if ($row['sendsms'] == 1)
					curl_request_async(
						'https://api.tropo.com/1.0/sessions',
						array('action'=>'create','token'=>'062ef2e3b4ea2447a858f2da36b703a973e2982944835a45a3aa15a79705847d7e4d806b231883ab256609b1','numberToDial' => $row['cellnumber'], 'msg' => $msg ),
						'GET');
				if ($row['sendemail'] == 1)
					mail($row['emailaddress'], "Appointment reminder from " . $userAccounts[$row['facebookid']]['company'], $msg, "From: " . $userAccounts[$row['facebookid']]['emailaddress']); 
				
			}
		}
	}
	mysql_query("UPDATE notifications SET notifydate='" . $notificationParams . "' WHERE entryid='" . $row['entryid'] . "'");
}

?>